<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表码" prop="tableCode">
        <el-input
          v-model="queryParams.tableCode"
          placeholder="请输入表码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="委托单位id" prop="enterpriseId">
        <el-input
          v-model="queryParams.enterpriseId"
          placeholder="请输入委托单位id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="委托单位名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入委托单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="委托人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入委托人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程id" prop="engineeringId">
        <el-input
          v-model="queryParams.engineeringId"
          placeholder="请输入工程id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程名称" prop="engineeringName">
        <el-input
          v-model="queryParams.engineeringName"
          placeholder="请输入工程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程地点" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入工程地点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建设单位" prop="constructionUnit">
        <el-input
          v-model="queryParams.constructionUnit"
          placeholder="请输入建设单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监理单位" prop="supervisionUnit">
        <el-input
          v-model="queryParams.supervisionUnit"
          placeholder="请输入监理单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设计单位" prop="designUnit">
        <el-input
          v-model="queryParams.designUnit"
          placeholder="请输入设计单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="勘察单位" prop="surveyUnit">
        <el-input
          v-model="queryParams.surveyUnit"
          placeholder="请输入勘察单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="施工单位" prop="construcUnit">
        <el-input
          v-model="queryParams.construcUnit"
          placeholder="请输入施工单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="委托日期" prop="commissionDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.commissionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择委托日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon=""
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
         
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :height="tableHeight" :data="commissionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="id" />
      <el-table-column label="表码" align="center" prop="tableCode" />
      <el-table-column label="编号" align="center" prop="number" />
      <el-table-column label="委托单位id" align="center" prop="enterpriseId" />
      <el-table-column label="委托单位名称" align="center" prop="enterpriseName" />
      <el-table-column label="委托人" align="center" prop="name" />
      <el-table-column label="联系电话" align="center" prop="mobile" />
      <el-table-column label="工程id" align="center" prop="engineeringId" />
      <el-table-column label="工程名称" align="center" prop="engineeringName" />
      <el-table-column label="工程地点" align="center" prop="address" />
      <el-table-column label="建设单位" align="center" prop="constructionUnit" />
      <el-table-column label="监理单位" align="center" prop="supervisionUnit" />
      <el-table-column label="设计单位" align="center" prop="designUnit" />
      <el-table-column label="勘察单位" align="center" prop="surveyUnit" />
      <el-table-column label="施工单位" align="center" prop="construcUnit" />
      <el-table-column label="工程概况" align="center" prop="overview" />
      <el-table-column label="检测内容" align="center" prop="detectionContent" />
      <el-table-column label="依据内容" align="center" prop="accordingContent" />
      <el-table-column label="委托日期" align="center" prop="commissionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.commissionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="pdf文件" align="center" prop="pdfFile" />
      <el-table-column label="word文件" align="center" prop="wordFile" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['commission:commission:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['commission:commission:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改委托对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表码" prop="tableCode">
          <el-input v-model="form.tableCode" placeholder="请输入表码" />
        </el-form-item>
        <el-form-item label="编号" prop="number">
          <el-input v-model="form.number" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="委托单位id" prop="enterpriseId">
          <el-input v-model="form.enterpriseId" placeholder="请输入委托单位id" />
        </el-form-item>
        <el-form-item label="委托单位名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入委托单位名称" />
        </el-form-item>
        <el-form-item label="委托人" prop="name">
          <el-input v-model="form.name" placeholder="请输入委托人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="工程id" prop="engineeringId">
          <el-input v-model="form.engineeringId" placeholder="请输入工程id" />
        </el-form-item>
        <el-form-item label="工程名称" prop="engineeringName">
          <el-input v-model="form.engineeringName" placeholder="请输入工程名称" />
        </el-form-item>
        <el-form-item label="工程地点" prop="address">
          <el-input v-model="form.address" placeholder="请输入工程地点" />
        </el-form-item>
        <el-form-item label="建设单位" prop="constructionUnit">
          <el-input v-model="form.constructionUnit" placeholder="请输入建设单位" />
        </el-form-item>
        <el-form-item label="监理单位" prop="supervisionUnit">
          <el-input v-model="form.supervisionUnit" placeholder="请输入监理单位" />
        </el-form-item>
        <el-form-item label="设计单位" prop="designUnit">
          <el-input v-model="form.designUnit" placeholder="请输入设计单位" />
        </el-form-item>
        <el-form-item label="勘察单位" prop="surveyUnit">
          <el-input v-model="form.surveyUnit" placeholder="请输入勘察单位" />
        </el-form-item>
        <el-form-item label="施工单位" prop="construcUnit">
          <el-input v-model="form.construcUnit" placeholder="请输入施工单位" />
        </el-form-item>
        <el-form-item label="工程概况" prop="overview">
          <el-input v-model="form.overview" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="检测内容">
          <editor v-model="form.detectionContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="依据内容">
          <editor v-model="form.accordingContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="创建来源" prop="createType">
          <el-select v-model="form.createType" placeholder="请选择创建来源">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="更新来源" prop="updateType">
          <el-select v-model="form.updateType" placeholder="请选择更新来源">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="委托日期" prop="commissionDate">
          <el-date-picker clearable size="small"
            v-model="form.commissionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择委托日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="pdf文件">
          <fileUpload v-model="form.pdfFile"/>
        </el-form-item>
        <el-form-item label="word文件">
          <fileUpload v-model="form.wordFile"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommission, getCommission, logicDelCommission, addCommission, updateCommission, exportCommission } from "@/api/commission/commission";

export default {
  name: "Commission",
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 委托表格数据
      commissionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableCode: null,
        number: null,
        enterpriseId: null,
        enterpriseName: null,
        name: null,
        mobile: null,
        engineeringId: null,
        engineeringName: null,
        address: null,
        constructionUnit: null,
        supervisionUnit: null,
        designUnit: null,
        surveyUnit: null,
        construcUnit: null,
        overview: null,
        detectionContent: null,
        accordingContent: null,
        commissionDate: null,
        pdfFile: null,
        wordFile: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.calHeight()
    },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    calHeight () {
          this.$nextTick(() => {
              const rect = this.$refs.appcontainer.getBoundingClientRect();
              let height= this.$refs.queryForm.$el.offsetHeight;
              this.tableHeight = rect.height-height-120;
          })
      },
    /** 查询委托列表 */
    getList() {
      this.loading = true;
      listCommission(this.queryParams).then(response => {
        this.commissionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tableCode: null,
        number: null,
        enterpriseId: null,
        enterpriseName: null,
        name: null,
        mobile: null,
        engineeringId: null,
        engineeringName: null,
        address: null,
        constructionUnit: null,
        supervisionUnit: null,
        designUnit: null,
        surveyUnit: null,
        construcUnit: null,
        overview: null,
        detectionContent: null,
        accordingContent: null,
        createTime: null,
        updateTime: null,
        delState: null,
        delUser: null,
        delType: null,
        delTime: null,
        createBy: null,
        createType: null,
        updateBy: null,
        updateType: null,
        commissionDate: null,
        pdfFile: null,
        wordFile: null,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加委托";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCommission(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改委托";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCommission(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommission(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelCommission(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有委托数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCommission(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
